﻿using DtoFw;
using DtoFw.Query;
using Microsoft.AspNetCore.Mvc;
using Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SceneEvidenceAPI.Controllers
{
    /// <summary>
    /// 字典
    /// </summary>
    [Route("api/dic")]
    public class MainDicController : BaseController
    {
        private readonly MainDicService _dicService;
        /// <summary>
        /// 字典
        /// </summary>
        /// <param name="dicService"></param>
        public MainDicController(MainDicService dicService)
        {
            this._dicService = dicService;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPost("post")]
        public BaseResult Post([FromBody] DicAddDto dto)
        {
            return _dicService.Post(dto);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPut("put")]
        public BaseResult Put([FromBody] DicUpdateDto dto)
        {
            return _dicService.Put(dto);
        }
        /// <summary>
        /// 根据父节点查询
        /// </summary>
        /// <param name="parentId">父节点ID</param>
        /// <returns></returns>
        [HttpGet("get/{parentId}")]
        public BaseGenericResult<List<DicDto>> QueryToParentId(long parentId)
        {
            return _dicService.QueryToParentId(parentId);
        }
        /// <summary>
        /// 根据父节点查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("get/{parentCode}/code")]
        public BaseGenericResult<List<DicDto>> QueryToParentCode(string parentCode)
        {
            return _dicService.QueryToParentCode(parentCode);
        }

        /// <summary>
        /// 根据父节点查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("get/yjpz/code")]
        public BaseGenericResult<object> QuerHardwareConfig()
        {
            var lst = new List<object>();
            var child = _dicService.QueryToParentCode("硬件配置");
            foreach (var item in child.Data)
            {
                dynamic t = new System.Dynamic.ExpandoObject();
                t.code = item.Code;
                t.title = item.Name;
                t.child = _dicService.QueryToParentId(item.Id).Data.Select(v => new { Name = v.Name, Code = v.Code });
                lst.Add(t);
            }
            return new BaseGenericResult<object>() { Data = lst, Success = true, Code = DtoFw.Enum.EnumReturnCode.Success };
        }


        /// <summary>
        /// 查询所有字典项
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpGet("get/all")]
        public BaseGenericResult<List<DicDto>> Query()
        {
            return _dicService.Query();
        }
        /// <summary>
        /// 父字典分页
        /// </summary>
        /// <param name="dicQuery"></param>
        /// <returns></returns>
        [HttpGet("get/pager")]
        public BaseGenericResult<Pager<DicDto>> QueryPager([FromQuery] DicQueryDto dicQuery)
        {
            return _dicService.QueryParentDic(dicQuery);
        }
        /// <summary>
        /// 查询单个
        /// </summary>
        /// <param name="id">字典ID</param>
        /// <returns></returns>
        [HttpGet("get/{id}/single")]
        public BaseGenericResult<DicDto> Get(long id)
        {
            return _dicService.Get(id);
        }
        /// <summary>
        /// 删除字典
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete")]
        public BaseResult Delete([FromBody] DeleteSingleDto query)
        {
            return _dicService.Delete(query);
        }
    }
}
